import redis

class MyRedis():
    def __init__(self):
        self.r = redis.Redis(host="127.0.0.1", port=6379, db=0)

    #字符串 添加、获取、删除、自增、自减
    def set_str(self,key,value):
        return self.r.set(key,value)

    # 加过期时间 times 单位为秒
    def setex_str(self,key,times,value):
        return self.r.setex(key,times,value)

    def get_str(self,key):
        value = self.r.get(key)
        if value:
            value = value.decode()
            # value = value.decode('utf-8')
        return value

    def incr_str(self,key,value):
        return self.r.incr(key,value)

    def delete_str(self,key):
        return self.r.delete(key)

    #hash
    def hash_add(self,key,field,value):
        return self.r.hset(key,field,value)

    def hash_getall(self,key):
        return self.r.hgetall(key)

    def hash_getone(self,key,field):
        return self.r.hget(key,field)

    def hash_hdel(self,key,field):
        return self.r.hdel(key,field)

    def get_keys(self,key):
        return self.r.keys(key)

    #list
    def list_push(self,key,value):
        return self.r.lpush(key,value)

    def list_pop(self,key):
        return self.r.rpop(key)

    def list_lrange(self,key,min,max):
        return self.r.lrange(key,min,max)

    def list_len(self,key):
        return self.r.llen(key)

    def list_del(self,key,field):
        return self.r.lrem(key,-1,field)

    # 原子性操作的方法
    def list_rpoplpush(self,list1,list2):
        return self.r.rpoplpush(list1,list2)

    #zset
    def zset_zadd(self,key,score,value):
        map = {score:value}
        return self.r.zadd(key,mapping=map)

    def zset_zrem(self,key,value):
        return self.r.zrem(key,value)

    def zset_zrangebyscore(self,key,min,max):
        return self.r.zrange(key,min,max)


# 单例模式
r = MyRedis()